//工具函数层（底层） 设置服务器地址/设置超时时间/请求拦截器/响应拦截器
//导入axios
import axios from "axios";

const service = axios.create({
  baseURL: "http://110.191.238.10:19638/",
  timeout: 100000,
});

//响应拦截
service.interceptors.response.use(
  (res) => {
    if(res.data?.code == '401'){
      let cancelArr = window.axiosCancel;
      cancelArr.forEach((ele, index) => {
        ele('取消请求')
        delete window.axiosCancel[index]
      })
      // let res = window.confirm("登录失效，请重新登录")
    }
    // 响应成功返回数据
    return res.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default function axiosReq({
  method,
  url,
  data,
  params,
  befLoading,
  aftLoading,
}) {
  return service({
    method: method || "get",
    url,
    data: data || {},
    params: params || "",
    befLoading: befLoading || false,
    aftLoading: aftLoading || true,
  });
}
